package org.example.controller;

import org.example.model.ApiResponse;
import org.example.model.BaseModel;
import org.example.service.BaseService;
import org.example.util.ApiResponseUtil;
import org.springframework.web.bind.annotation.*;

public abstract class BaseController<T extends BaseModel, S extends BaseService<T>> {

    protected final S service;
    public BaseController(S service) {
        this.service = service;
    }

    /*@GetMapping("/all")
    public ApiResponse<List<T>> getAll() {
        List<T> list= service.getAll();
        return ApiResponseUtil.success(list);
    }*/

    @GetMapping("/get/{id}")
    public ApiResponse<T> getOne(@PathVariable Long id) {
        return ApiResponseUtil.success(service.getOne(id));
    }

    @PostMapping("/create")
    public ApiResponse<Long> create(@RequestBody T entity) {
        Long id=service.create(entity);
        return ApiResponseUtil.success(id);
    }

    @PutMapping("/update")
    public ApiResponse<Void> update(@RequestBody T entity) {
        service.update(entity);
        return ApiResponseUtil.success(null);
    }

    @DeleteMapping("/delete/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return ApiResponseUtil.success(null);
    }
}
